package com.example.rubbish.Fragment;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Toast;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;

import com.example.rubbish.DataBase.HDataBase;
import com.example.rubbish.DataBase.History;
import com.example.rubbish.R;
import com.example.rubbish.ResultActivity;
import com.example.rubbish.SearchActivity;
import com.example.rubbish.Ui.NumberTextView;
import com.example.rubbish.Utils.JsonUtils;
import com.example.rubbish.Utils.okhttpUtils;
import com.example.rubbish.gson.Hot;
import com.google.android.flexbox.FlexboxLayout;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.Timer;
import java.util.TimerTask;

import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.Response;

public class HotFragment extends Fragment {
    private List<NumberTextView> newsList= new ArrayList<>();
    private List<String> nameList= new ArrayList<>();
    private static final int SUCCESS=200;
    private static final int NUMBER_GET=10;
    private HDataBase db;

    private HistoryFragment historyFragment;
    private FlexboxLayout box_history;


    private View mView;
    private Context mContext;
    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        mView = inflater.inflate(R.layout.fragment_hot,container,false);
        mContext=getContext();
        historyFragment = (HistoryFragment) ((SearchActivity)mContext).getSupportFragmentManager().findFragmentById(R.id.part_history);
//        box_history=historyFragment.getView().findViewById(R.id.box_history);

        db=HDataBase.getInstance(getContext());
        initView();
        hotRequestSetView();

        return mView;
    }




    @Override
    public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);




    }//onViewCreated

    private void setViewOnClickListener() {
        NumberTextView news = new NumberTextView(getContext());

        for(int i=0;i<NUMBER_GET;i++) {
            news=newsList.get(i);
          String name=nameList.get(i);

            news.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    Intent intent = new Intent(getContext(), ResultActivity.class);
                    intent.putExtra("searchContent", name);
                    intent.putExtra("searchType",0);

                    History history = new History();
                    history.setHistoryName(name);
                    db.saveHistory(history);

                    startActivity(intent);
                }
            });
        }
    }


    private void setNewsText(String name,int i) {
        nameList.add(name);
        newsList.get(i).setTextView(name);

    }

    private void initView() {
        NumberTextView news1 = mView.findViewById(R.id.news1);
        NumberTextView news2 = mView.findViewById(R.id.news2);
        NumberTextView news3 = mView.findViewById(R.id.news3);
        NumberTextView news4 = mView.findViewById(R.id.news4);
        NumberTextView news5 = mView.findViewById(R.id.news5);
        NumberTextView news6 = mView.findViewById(R.id.news6);
        NumberTextView news7 = mView.findViewById(R.id.news7);
        NumberTextView news8 = mView.findViewById(R.id.news8);
        NumberTextView news9 = mView.findViewById(R.id.news9);
        NumberTextView news10 = mView.findViewById(R.id.news10);
        newsList.add(news1);
        newsList.add(news2);
        newsList.add(news3);
        newsList.add(news4);
        newsList.add(news5);
        newsList.add(news6);
        newsList.add(news7);
        newsList.add(news8);
        newsList.add(news9);
        newsList.add(news10);
    }

    private void hotRequestSetView() {
        okhttpUtils.hotNewsRequest(new Callback() {
            @Override
            public void onFailure(@NonNull Call call, @NonNull IOException e) {
                ((SearchActivity)mContext).runOnUiThread(new Runnable() {
                    //不能http ,非https，被Android禁止了
                    @Override
                    public void run() {
                        Toast.makeText(mContext,"服务获取异常，请稍后再试",Toast.LENGTH_LONG).show();
                    }
                });
            }

            @Override
            public void onResponse(@NonNull Call call, @NonNull Response response) throws IOException {
                final String responseText = response.body().string();
                Log.i("HotFrag", responseText);
                final Hot hot = JsonUtils.handleHotResponse(responseText);
                ((SearchActivity)mContext).runOnUiThread(new Runnable() {
                    @Override
                    public void run() {
                        if(hot !=null && hot.getCode()==SUCCESS) {
                            for(int i=0;i<NUMBER_GET;i++) {
                                setNewsText(hot.getNewsAt(i).getName(),i);
                            }
                            setViewOnClickListener();

                        }else {

                            Toast errorWarn = Toast.makeText(getContext(),"请求为空",Toast.LENGTH_LONG);
                            errorWarn.setGravity(Gravity.CENTER,0,0);
                            errorWarn.show();
                            new Timer().schedule(new TimerTask() {
                                @Override
                                public void run() {
                                    //失败页面,不自动返回搜索页面
                                    //to do something
                                }
                            },500);
                        }


                    }//run()

                });
            }
        });//okhttpUtils.hotNewsRequest(
    }



}//class




